﻿package com.sz.courseserial.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.sz.common.ConstDefine;
import com.sz.common.ICommonDAO;
import com.sz.login.bmo.ILoginBMO;
import com.sz.manager.dao.IManagerDAO;
import com.sz.courseserial.bmo.ICourseSerialBMO;

import org.springframework.web.servlet.mvc.Controller;

public class OperateCourseSerial extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			String type = request.getParameter("OPERATE_TYPE");
			
			if(type.compareToIgnoreCase("EDIT") == 0) {
				editCourseSerial(request, response);
			} else if(type.compareToIgnoreCase("EDIT2") == 0) {
				editCourseSerial2(request, response);
			} else if(type.compareToIgnoreCase("RUSH") == 0) {
				rushStatus(request, response);
			} else if(type.compareToIgnoreCase("DEL") == 0) {
				String rtn_type = request.getParameter("RETURNTYPE");
				delCourseSerial(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("DEL2") == 0) {
				String rtn_type = request.getParameter("RETURNTYPE");
				delCourseSerial2(request, response, rtn_type);
			}
			/*
			if(type.compareToIgnoreCase("LOCK") == 0) {
				lockTicket(request, response);				
			} else if(type.compareToIgnoreCase("EDIT") == 0) {
				editCourseSerial(request, response);
			} else if(type.compareToIgnoreCase("NEXTSTATUS") == 0) {
				nextStatus(request, response);
			} else if(type.compareToIgnoreCase("CANCEL") == 0) {
				String rtn_type = request.getParameter("RETURNTYPE");
				cancelTicket(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("CONFIRMFAILED") == 0) {
				String rtn_type = request.getParameter("RETURNTYPE");
				confirmfailedTicket(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("CHECK") == 0) {				
				checkTicket(request, response);
			} else if(type.compareToIgnoreCase("CHECK") == 0) {				
				checkTicket(request, response);
			}*/
	}

	@SuppressWarnings("unchecked")
	private void delCourseSerial(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
	throws ServletException, IOException{
		String path = request.getContextPath();
		
		try {				
			String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
			
			String json = request.getParameter("PARAM");
			Map param = (Map)JSONSerializer.toJSON(json);

			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			ICourseSerialBMO courseserial = (ICourseSerialBMO) context.getBean("CourseSerialBMO");
			String msg = courseserial.delCourseSerial(param);
		
			//response.setCharacterEncoding("utf-8");
			//response.getWriter().print("撤单成功");
			/*
			if(msg == null) {					
				response.sendRedirect(path + "/page/success.jsp");
			} else {
				msg = URLEncoder.encode(msg, "UTF-8");
				response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
			}
	*/
			response.setCharacterEncoding("utf-8");
			response.getWriter().print("删除成功!");
			//response.sendRedirect(path + "/page/success.jsp");
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());		
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private void delCourseSerial2(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
	throws ServletException, IOException{
		String path = request.getContextPath();
		
		try {				
			String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
			
			String json = request.getParameter("PARAM");
			Map param = (Map)JSONSerializer.toJSON(json);

			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			ICourseSerialBMO courseserial = (ICourseSerialBMO) context.getBean("CourseSerialBMO");
			String msg = courseserial.delCourseSerial2(param);
		
			//response.setCharacterEncoding("utf-8");
			//response.getWriter().print("撤单成功");
			/*
			if(msg == null) {					
				response.sendRedirect(path + "/page/success.jsp");
			} else {
				msg = URLEncoder.encode(msg, "UTF-8");
				response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
			}
	*/
			response.setCharacterEncoding("utf-8");
			response.getWriter().print("删除成功!");
			//response.sendRedirect(path + "/page/success.jsp");
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());		
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private void rushStatus(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException{
		String path = request.getContextPath();
		
	}
	
	@SuppressWarnings("unchecked")
	private void lockTicket(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
	}
	
	@SuppressWarnings("unchecked")
	private void editCourseSerial(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ICourseSerialBMO courseserial = (ICourseSerialBMO) context.getBean("CourseSerialBMO");
				String msg = courseserial.editCourseSerial(param);
				
				String path = request.getContextPath(); 
				if(msg == null) {					
					response.sendRedirect(path + "/page/success.jsp");
				} else {
					msg = URLEncoder.encode(msg, "UTF-8");
					response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void editCourseSerial2(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ICourseSerialBMO courseserial = (ICourseSerialBMO) context.getBean("CourseSerialBMO");
				String msg = courseserial.editCourseSerial2(param);
				
				String path = request.getContextPath(); 
				if(msg == null) {					
					response.sendRedirect(path + "/page/success.jsp");
				} else {
					msg = URLEncoder.encode(msg, "UTF-8");
					response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void nextStatus(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{

	}
	@SuppressWarnings("unchecked")
	private void cancelTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
					
			
	}
	
	@SuppressWarnings("unchecked")
	private void confirmfailedTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
				
				
	}
	
	@SuppressWarnings("unchecked")
	private void checkTicket(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
}
